package com.author.surface;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.plaf.basic.BasicSliderUI;

public class ExtendedSliderUI extends BasicSliderUI {
	/* Track listener when the mouse is dragged over the track of the slider */
	protected BasicSliderUI.TrackListener	trackListener	= new ExtendedSliderTrackListener();
	/*
	 * The Text panel on which the vertical ruler line will be drawn when the
	 * user presses the thumb of the slider
	 */
	protected TextPanel						textPanel;

	public ExtendedSliderUI(JSlider b) {
		super(b);
	}

	protected BasicSliderUI.TrackListener createTrackListener(JSlider slider) {
		return this.trackListener;
	}

	public void paintLabels(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		/* set rendering hints so that the label texts are anti-aliased */
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintLabels(g2d);
	}

	public void paintTicks(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		/* set rendering hints so that the paint ticks are anti-aliased */
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintTicks(g2d);
	}

	protected class ExtendedSliderTrackListener extends
			BasicSliderUI.TrackListener {
		/* Color of the Ruler drawn on the surface of the Text Panel */
		private final Color	rulerColor	= new Color(99, 225, 255);

		public ExtendedSliderTrackListener() {
			super();
		}

		@Override
		public void mousePressed(MouseEvent e) {
			super.mousePressed(e);
			Graphics2D g2d = (Graphics2D) ExtendedSliderUI.this.textPanel
					.getCanvasPanel().getGraphics();

			/* set color of the ruler and its anti-aliased property */
			g2d.setColor(this.rulerColor);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			/*
			 * The 150 subtraction from the Y coordinate will ensure that the
			 * blue Ruler will extend to the top of the canvas panel
			 */
			g2d.drawLine(e.getXOnScreen(), e.getYOnScreen() - 150,
					e.getXOnScreen(), e.getYOnScreen()
							+ ExtendedSliderUI.this.textPanel.getCanvasPanel()
									.getHeight());
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			super.mouseReleased(e);
			/* Update entire text panel when mouse is released */
			Graphics2D gd = (Graphics2D) ExtendedSliderUI.this.textPanel
					.getCanvasPanel().getGraphics();
			gd.setColor(rulerColor);
			ExtendedSliderUI.this.textPanel.getCanvasPanel().update(gd);
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			super.mouseDragged(e);
			Graphics2D g2d = (Graphics2D) ExtendedSliderUI.this.textPanel
					.getCanvasPanel().getGraphics();
			g2d.setColor(rulerColor);
			ExtendedSliderUI.this.textPanel.getCanvasPanel().update(g2d);

			/* redraw the ruler at the current mouse position */
			/* set color of the ruler and its anti-aliased property */
			g2d.setColor(this.rulerColor);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			/*
			 * The 150 subtraction from the Y coordinate will ensure that the
			 * blue Ruler will extend to the top of the canvas panel
			 */
			g2d.drawLine(e.getXOnScreen(), e.getYOnScreen() - 150,
					e.getXOnScreen(), e.getYOnScreen()
							+ ExtendedSliderUI.this.textPanel.getHeight());
		}
	}

	/**
	 * Set the panel for the slider to draw ruler lines when the user presses
	 * the mouse button
	 * 
	 * @param panel
	 *            on which the ruler lines will be drawn
	 */
	public void setPanelForSlider(JPanel panel) {
		if (panel == null) {
			throw new RuntimeException("Invalid canvas panel reference");
		}
		this.textPanel = (TextPanel) panel;
	}
}
